<?php

$users = array(
    'john' => array(
        'id' => 'john',
        'name' => '张三',
        'pwd' => '1234',
    ),
    'mark' => array(
        'id' => 'mark',
        'name' => '李四',
        'pwd' => '1234',
    ),
);

function valid_user($uname, $upwd) {
    global $users;
    $ErrMsg = null;
    if (array_key_exists($uname, $users)) {       //用户名正确
        if ($users[$uname]['pwd'] === $upwd) {     //密码正确
            $_SESSION['user'] = $users[$uname];    //把登录的用户数组放到SESSION
            $_SESSION['cart'] = array();           //分配一个空的购物车
        } else {  //密码错误
            $ErrMsg = "密码错误!";
        }
    } else { //用户名错误
        $ErrMsg = "用户名不存在!";
    }
    return $ErrMsg;
}
